package com.bjxst;

import java.io.*;

/** 通过字节流读取文本文件并添加行号*/
/*用字节流读取文本文件，但不可以直接添加行号，先把字节流转换为字符流，
* 再把字符流转换为字符缓冲流，这才能读一行，
* 用到缓冲字符流BufferReader特有的方法readLine()*/
public class LineNumberDemo2 {
    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try{
            //FileInputStream是字节流，BufferedReader是字符流，所以报错
           // br = new BufferedReader(new FileInputStream("d;/b.txt"))
            br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/b.txt")));
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:/g.txt")));
            String temp = "";
            int i = 1;
            while((temp = br.readLine() )!= null){
                bw.write(i+","+temp);
                bw.newLine();//换行
                i++;
            }
            bw.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(br != null){
                    br.close();
                }
                if(bw != null){
                    bw.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
